home *** CD-ROM | disk | FTP | other *** search
/ Packard Bell - Internet on a CD / internet on a cd.cdr / Internet / sites / Clementine_NASA / pds.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-16  |  11.3 KB  |  464 lines

  1. /*  
  2.  *    THIS ROUTINE IS PART OF THE CLEMENTINE PDS FILE READER PROGRAM.
  3.  *    IT WAS WRITTEN BY ACT CORP. IN DIRECT SUPPORT TO THE 
  4.  *    CLEMENTINE (DSPSE) PROGRAM.
  5.  *
  6.  *    IF YOU FIND A PROBLEM OR MAKE ANY CHANGES TO THIS CODE PLEASE CONTACT
  7.  *    Dr. Erick Malaret at ACT Corp. 
  8.  *            tel: (703) 742-0294
  9.  *                 (703) 683-7431
  10.  *                       email:    nrlvax.nrl.navy.mil
  11.  *    
  12.  *
  13.  */
  14. #include <stdio.h>
  15. #include <math.h>
  16. #ifdef __TURBOC__
  17. #include <alloc.h>
  18. #else
  19. #include <malloc.h>
  20. #endif
  21. #include <string.h>
  22. #include "jpeg_c.h"
  23. #include "pds.h"
  24.  
  25. #define READ_PARAM "rb"
  26. #define WRITE_PARAM "wb"
  27.  
  28. PDSINFO    pds;
  29. FILE    *qparm;
  30.  
  31. void init_qt(FILE *fptr);
  32. void readhufftbls(FILE *fptr);
  33. void pds_decomp(FILE *fptr,CHARH *p,long sizej,long sizei);
  34. #ifdef    sun 
  35. void PClong2SUNlongVector(unsigned long invec[],int npts) ;
  36. void PCshort2SUNshortVector(unsigned short invec[],int npts) ;
  37. #endif
  38.  
  39.  
  40. PDSINFO *PDSR(char *fname, long *rows, long *cols)
  41. {
  42.     FILE    *fptr;
  43.     int     n;
  44.     int     j,i;
  45.     CHARH   *c;
  46.     char    nstring[84],sdummy[80],buffer[10],low;
  47.     long    sizej;                  /* number of rows in the image */
  48.     long    sizei;                  /* number of columns in the image */
  49.     int     bitpix;                 /* bits per pixel */
  50.     int     record_bytes;
  51.     int     hist_rec,brw_rec,image_rec,ns,brwsize;
  52.     char    cval, *sptr, *ptr;
  53.     char    record_type[20];
  54.     int     COMPRESSED;
  55.     long    k,hdr_size;
  56.  
  57.     if( (fptr = fopen(fname,READ_PARAM)) == NULL){  /* open disk file */
  58.         printf("Can't open %s.\n",fname);
  59.         return NULL;
  60.     }
  61.  
  62.     /* initialize some basic variables */
  63.     bitpix            =0;
  64.     sizej=sizei        =0;
  65.     pds.browse_nrows    =0;
  66.     pds.browse_ncols    =0;
  67.     pds.image_nrows        =0;
  68.     pds.image_ncols        =0;
  69.     hist_rec = brw_rec = image_rec = -1;
  70.  
  71.     /* read header */
  72.     do{
  73.         /* read next line of text */
  74.         for (n=0; (cval=fgetc(fptr)); n++) {
  75.             nstring[n]=cval;
  76.             if(cval=='\n') {
  77.                 if( (cval=fgetc(fptr)) != '\r') ungetc(cval,fptr);
  78.                 nstring[++n]='\0';
  79.                 break;
  80.             }
  81.         }
  82.  
  83.         /* find line's first non-space character */
  84.         for (ns=0; nstring[ns]==' ';ns++);
  85.         sptr = &nstring[ns];
  86.  
  87.         if (strncmp("^IMAGE_HISTOGRAM ",sptr,17)==0) {
  88.                     /*printf("image histogram found \n"); */ 
  89.             n=sscanf(nstring,"%s = %d", sdummy, &hist_rec);
  90.                         /*printf("hist_rec = %d\n",hist_rec);*/
  91.         }
  92.  
  93.         if (strncmp("^BROWSE_IMAGE ",sptr,14)==0) {
  94.             n=sscanf(nstring,"%s = %d", sdummy, &brw_rec);
  95.                     /*printf("brw_rec = %d\n",brw_rec);*/
  96.         }
  97.  
  98.         if (strncmp("^IMAGE ",sptr,7)==0) {
  99.             n=sscanf(nstring,"%s = %d", sdummy, &image_rec);
  100.         }
  101.  
  102.         if (strncmp("RECORD_TYPE",sptr,9)==0) {
  103.             n=sscanf(nstring,"%s = %s", sdummy, &record_type);
  104.         }
  105.  
  106.         if (strncmp("RECORD_BYTES",sptr,12)==0) {
  107.             n=sscanf(nstring,"%s = %d", sdummy, &record_bytes);
  108.         }
  109.  
  110.         if (strncmp("ENCODING_TYPE",sptr,13)==0) {
  111.             n=sscanf(nstring,"%s = %s", sdummy, buffer);
  112.             if ( strstr(buffer,"JPEG") )
  113.                 COMPRESSED = 1;
  114.             else
  115.                 COMPRESSED = 0;
  116.         }
  117.  
  118.         if (strncmp("LINES ",sptr,6)==0) {
  119.             n=sscanf(nstring,"%s = %d", sdummy, &sizej);
  120.         }
  121.  
  122.         if (strncmp("LINE_SAMPLES",sptr,12)==0) {
  123.             n=sscanf(nstring,"%s = %d", sdummy, &sizei);
  124.         }
  125.  
  126.         if (strncmp("SAMPLE_BITS",sptr,11)==0) {
  127.             n=sscanf(nstring,"%s = %d", sdummy, &bitpix);
  128.         }
  129.  
  130.         if (strncmp("END",sptr,3)==0) {
  131.             if ( *(sptr+3)=='\n' || *(sptr+3)==' ' || *(sptr+3)=='\r' ) {
  132.                 hdr_size = ftell(fptr);
  133.                 break;
  134.             }
  135.         }
  136.     } while(1);
  137.  
  138.     /**************   read histogram  ***************/
  139.     if ( hist_rec != -1 ) {
  140.         fseek(fptr,hist_rec-1,0);
  141.         pds.hist = (long *)malloc(256*sizeof(long));
  142.         if(pds.hist == NULL){
  143.                          printf(" histogram memory not allocated \n"); 
  144.                 }
  145.         if ( pds.hist ){
  146.               fread(pds.hist,sizeof(long),256,fptr);
  147. #ifdef sun
  148.               PClong2SUNlongVector((unsigned long*)pds.hist,256);
  149. #endif
  150.                     /*printf("pds.hist = %x\n",pds.hist);*/ 
  151.                 }
  152.     }
  153.  
  154.     /**************   read browse image **********/
  155.         if ( brw_rec != -1 ) {
  156.             pds.browse_ncols    = sizei/8;
  157.                         pds.browse_nrows    = sizej/8;
  158.             fseek(fptr,brw_rec-1,0);
  159.             brwsize = (sizej/8) * (sizei/8);
  160.             pds.brw_imag = (unsigned char *)malloc(brwsize);
  161.             if ( pds.brw_imag )
  162.                 fread(pds.brw_imag,sizeof(char),brwsize,fptr);
  163.         }
  164.  
  165.     /*************   read image data ***************/
  166.     if (strncmp(record_type,"UNDEFINED",9)==0) {
  167.         record_bytes=1;
  168.         fseek(fptr,(image_rec-1),0);
  169.     } else {
  170.         fseek(fptr,(image_rec-1)*record_bytes,0);
  171.     }
  172.  
  173.     switch (bitpix) {
  174.     case 8:
  175. #ifdef __TURBOC__
  176.         c = (CHARH *)farmalloc(sizej*sizei);
  177. #else
  178.         c = (CHARH *)malloc(sizej*sizei);
  179. #endif
  180.         if ( c == NULL ) {
  181.             printf("Can't allocate memory for image array.\n");
  182.             fclose(fptr);
  183.             return NULL;
  184.         }
  185.  
  186.         if ( COMPRESSED ) {
  187.             qparm = fopen("paramtrs.dat","w");
  188.             init_qt(fptr);
  189.             readhufftbls(fptr);
  190.             pds_decomp(fptr,c,sizej,sizei);
  191.             fclose(qparm);
  192.         } else {
  193.                     
  194.             for (j=0, k=0; j<sizej ;j++) {
  195.                 for (i=0; i<sizei; i++) {
  196.                     /*low= fgetc(fptr); replace fgetc by fread 1/24/94 */
  197.                                         if(1!=fread(&low,sizeof(char),1,fptr)){
  198.                         printf("error: possible EOF found at (%d,%d)\n",j,i);
  199.                         break;
  200.                     } else {
  201.                         c[k++]=(unsigned char) low;
  202.                     }
  203.                 }
  204.             }
  205.         }
  206.  
  207.         pds.image = c;
  208.         break;
  209.     default:
  210.         printf("invalid number of bits per pixel\n");
  211.         pds.image = NULL;
  212.     }
  213.  
  214.     /************    Allocate string buffer    **************/
  215.     rewind(fptr);
  216.  
  217.     pds.text = (char *)malloc(hdr_size);
  218.     if ( pds.text ) {
  219.         for (ptr=pds.text,i=0; i<hdr_size; i++) {
  220.                /*    *(ptr) = fgetc(fptr); */
  221.             fread(ptr,sizeof(char),1,fptr);
  222.             if ( *ptr=='\r') {
  223.                 /* do nothing */
  224.             } else {
  225.                 ptr++;
  226.             }
  227.         }
  228.     }
  229.     *(ptr)='\0';
  230.  
  231.         /*****/
  232.     fclose(fptr);
  233.  
  234.     *rows = pds.image_nrows=sizej;
  235.     *cols = pds.image_ncols=sizei;
  236.  
  237.     return &pds;
  238. }
  239.  
  240.  
  241. /******** Routines that deal with compressed images *******/
  242. float   dfac[8] = {
  243. 0.35355339,0.35355339,0.653281482,0.27059805,
  244. 0.449988111,0.254897789,0.300672443,1.281457724
  245. };
  246.  
  247. void init_qt(FILE *fptr)
  248. {
  249.     short   i;
  250.     short   scalef,index;
  251.     short   table[64];
  252.     float   ftable[64];
  253.  
  254.     fread(&scalef,sizeof(short),1,fptr);
  255. #ifdef sun
  256.     PCshort2SUNshortVector((unsigned short*)&scalef,1) ;
  257. #endif
  258.     fprintf(qparm,"tabf: %d\n",scalef);
  259.     fread(table,sizeof(short),64,fptr);
  260. #ifdef sun
  261.     PCshort2SUNshortVector((unsigned short*)table,64);
  262. #endif
  263.     fprintf(qparm,"tabq:\n");
  264.  
  265.     for (i=0; i<64; i++) {
  266.         fprintf(qparm,"%3d ",table[i]);
  267.         if ( (i+1) % 8 == 0 ) fprintf(qparm,"\n");
  268.  
  269.         ftable[i] = (float)floor((float)scalef*table[i]/64.0 + 0.5);
  270.         ftable[i] = 4096.0 / ftable[i];
  271.     }
  272.  
  273.     ftable[0] = dfac[0]*dfac[0]*ftable[0];
  274.     ftable[32] = dfac[0]*dfac[1]*ftable[32];
  275.     ftable[16] = dfac[0]*dfac[2]*ftable[16];
  276.     ftable[48] = dfac[0]*dfac[3]*ftable[48];
  277.     ftable[8] = -dfac[0]*dfac[4]*ftable[8];
  278.     ftable[24] = -dfac[0]*dfac[5]*ftable[24];
  279.     ftable[56] = dfac[0]*dfac[6]*ftable[56];
  280.     ftable[40] = -dfac[0]*dfac[7]*ftable[40];
  281.  
  282.     ftable[4] = dfac[1]*dfac[0]*ftable[4];
  283.     ftable[36] = dfac[1]*dfac[1]*ftable[36];
  284.     ftable[20] = dfac[1]*dfac[2]*ftable[20];
  285.     ftable[52] = dfac[1]*dfac[3]*ftable[52];
  286.     ftable[12] = -dfac[1]*dfac[4]*ftable[12];
  287.     ftable[28] = -dfac[1]*dfac[5]*ftable[28];
  288.     ftable[60] = dfac[1]*dfac[6]*ftable[60];
  289.     ftable[44] = -dfac[1]*dfac[7]*ftable[44];
  290.  
  291.     ftable[2] = dfac[2]*dfac[0]*ftable[2];
  292.     ftable[34] = dfac[2]*dfac[1]*ftable[34];
  293.     ftable[18] = dfac[2]*dfac[2]*ftable[18];
  294.     ftable[50] = dfac[2]*dfac[3]*ftable[50];
  295.     ftable[10] = -dfac[2]*dfac[4]*ftable[10];
  296.     ftable[26] = -dfac[2]*dfac[5]*ftable[26];
  297.     ftable[58] = dfac[2]*dfac[6]*ftable[58];
  298.     ftable[42] = -dfac[2]*dfac[7]*ftable[42];
  299.  
  300.     ftable[6] = dfac[3]*dfac[0]*ftable[6];
  301.     ftable[38] = dfac[3]*dfac[1]*ftable[38];
  302.     ftable[22] = dfac[3]*dfac[2]*ftable[22];
  303.     ftable[54] = dfac[3]*dfac[3]*ftable[54];
  304.     ftable[14] = -dfac[3]*dfac[4]*ftable[14];
  305.     ftable[30] = -dfac[3]*dfac[5]*ftable[30];
  306.     ftable[62] = dfac[3]*dfac[6]*ftable[62];
  307.     ftable[46] = -dfac[3]*dfac[7]*ftable[46];
  308.  
  309.     ftable[1] = -dfac[4]*dfac[0]*ftable[1];
  310.     ftable[33] = -dfac[4]*dfac[1]*ftable[33];
  311.     ftable[17] = -dfac[4]*dfac[2]*ftable[17];
  312.     ftable[49] = -dfac[4]*dfac[3]*ftable[49];
  313.     ftable[9] = dfac[4]*dfac[4]*ftable[9];
  314.     ftable[25] = dfac[4]*dfac[5]*ftable[25];
  315.     ftable[57] = -dfac[4]*dfac[6]*ftable[57];
  316.     ftable[41] = dfac[4]*dfac[7]*ftable[41];
  317.  
  318.     ftable[3] = -dfac[5]*dfac[0]*ftable[3];
  319.     ftable[35] = -dfac[5]*dfac[1]*ftable[35];
  320.     ftable[19] = -dfac[5]*dfac[2]*ftable[19];
  321.     ftable[51] = -dfac[5]*dfac[3]*ftable[51];
  322.     ftable[11] = dfac[5]*dfac[4]*ftable[11];
  323.     ftable[27] = dfac[5]*dfac[5]*ftable[27];
  324.     ftable[59] = -dfac[5]*dfac[6]*ftable[59];
  325.     ftable[43] = dfac[5]*dfac[7]*ftable[43];
  326.  
  327.     ftable[7] = dfac[6]*dfac[0]*ftable[7];
  328.     ftable[39] = dfac[6]*dfac[1]*ftable[39];
  329.     ftable[23] = dfac[6]*dfac[2]*ftable[23];
  330.     ftable[55] = dfac[6]*dfac[3]*ftable[55];
  331.     ftable[15] = -dfac[6]*dfac[4]*ftable[15];
  332.     ftable[31] = -dfac[6]*dfac[5]*ftable[31];
  333.     ftable[63] = dfac[6]*dfac[6]*ftable[63];
  334.     ftable[47] = -dfac[6]*dfac[7]*ftable[47];
  335.  
  336.     ftable[5] = -dfac[7]*dfac[0]*ftable[5];
  337.     ftable[37] = -dfac[7]*dfac[1]*ftable[37];
  338.     ftable[21] = -dfac[7]*dfac[2]*ftable[21];
  339.     ftable[53] = -dfac[7]*dfac[3]*ftable[53];
  340.     ftable[13] = dfac[7]*dfac[4]*ftable[13];
  341.     ftable[29] = dfac[7]*dfac[5]*ftable[29];
  342.     ftable[61] = -dfac[7]*dfac[6]*ftable[61];
  343.     ftable[45] = dfac[7]*dfac[7]*ftable[45];
  344.  
  345.     for (i=0; i<64; i++) qtable[i] = ftable[zzseq[i]];
  346. }
  347.  
  348. void readhufftbls(FILE *fptr)
  349. {
  350.     fread(dcbits,sizeof(short),16,fptr);
  351. #ifdef sun
  352.     PCshort2SUNshortVector((unsigned short*)dcbits,16);
  353. #endif
  354.  
  355.     fread(dchuffval,sizeof(char),12,fptr);
  356.     fread(acbits,sizeof(short),16,fptr);
  357. #ifdef sun
  358.     PCshort2SUNshortVector((unsigned short*)acbits,16);
  359. #endif
  360.  
  361.     fread(achuffval,sizeof(char),162,fptr);
  362.  
  363.     inithuffcode();
  364. }
  365.  
  366.  
  367. void pds_decomp(FILE *fptr,CHARH *p,long sizej,long sizei)
  368. {
  369.     BitStream       ibs;
  370.     short   i, npanels;
  371.     long    nbytes,bytesperpanel;
  372.     short   blocks, rem;
  373.     unsigned short  nb;
  374.     long    filepos1, filepos2;
  375.     CHARH   *ptr;
  376.     int     FLAG = 0;
  377.  
  378.     cBitStream(&ibs,NULL,INPUT);
  379.  
  380.     filepos1 = ftell(fptr);
  381.     fseek(fptr,0,2);
  382.     filepos2 = ftell(fptr);
  383.     fseek(fptr,filepos1,0);
  384.  
  385.     nbytes = filepos2 - filepos1;
  386.  
  387. #ifdef __TURBOC__
  388.     ibs.outstring = (CHARH *)farmalloc(nbytes);
  389. #else
  390.     ibs.outstring = (CHARH *)malloc(nbytes);
  391. #endif
  392.     if ( ibs.outstring ) {
  393.         blocks = 1;
  394.         rem = 0;
  395.         nb = (unsigned short)nbytes;
  396.         if ( nbytes > 60000L ) {
  397.             blocks = nbytes / 32768;
  398.             rem = nbytes % 32768;
  399.             nb = 32768;
  400.         };
  401.         ptr = ibs.outstring;
  402.         for (i=0; i < blocks; i++,ptr+=nb) {
  403.             if ( fread(ptr,sizeof(char),nb,fptr) != nb ) {
  404.                 printf("Error reading data string.\n");
  405.                 FLAG = 1;
  406.                 break;
  407.             }
  408.         }
  409.         if ( rem ) {
  410.             if ( fread(ptr,sizeof(char),rem,fptr) != rem ) {
  411.                 printf("Error reading data string.\n");
  412.                 FLAG = 1;
  413.             }
  414.         }
  415.         ibs.mode = MEMORY;
  416.     } else {
  417.         ibs.bytestream.file = fptr;
  418.         ibs.mode = DISK;
  419.     }
  420.  
  421.     if ( !FLAG ) {
  422.         npanels = sizej/32;
  423.         bytesperpanel = 32*sizei;
  424.         for (i=0; i<npanels; i++,p+=bytesperpanel) {
  425.             decomp(&ibs,p,32,sizei);
  426.         }
  427.     }
  428.  
  429.     if ( ibs.outstring )
  430. #ifdef __TURBOC__
  431.         farfree( ibs.outstring );
  432. #else
  433.         free( ibs.outstring );
  434. #endif
  435. }
  436.  
  437.  
  438. #ifdef sun
  439. void PClong2SUNlongVector(unsigned long invec[],int npts){
  440.     int    i;
  441.     unsigned long     ival,oval;
  442.  
  443.     for(i=0;i<npts;i++){
  444.         ival    = invec[i];
  445.         oval    = ((ival&0x000000ff)<<24) +
  446.               ((ival&0x0000ff00)<<8) +
  447.               ((ival&0x00ff0000)>>8) +
  448.               ((ival&0xff000000)>>24);
  449.         invec[i]= oval; 
  450.     }
  451. }
  452. void PCshort2SUNshortVector(unsigned short invec[],int npts){
  453.     int    i;
  454.         unsigned short     ival,oval;
  455.      for(i=0;i<npts;i++){
  456.         ival    = invec[i];
  457.         oval    = (ival<<8) + ((ival>>8)&0x00ff);
  458.         invec[i]= oval;
  459.  
  460.         }
  461. }
  462. #endif
  463.  
  464.